package com.sang.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.sang.pojo.User;
import com.sang.utils.JsonUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RestController//加了这个注解，返回的数据就不会走视图解析器了，会直接返回一个字符窜,添加了这个就可以不用再添加@ResponseBody
public class UserController {

    @RequestMapping("/j1")
    @ResponseBody//加了这个注解，返回的数据就不会走视图解析器了，会直接返回一个字符窜
    public String json1() {
        //创建一个对象
        User user = new User("秦疆1号",3,"男");
        return user.toString();
    }

    //@RequestMapping(value = "/j2",produces = "application/json;charset=utf-8")
    //由于上面配置太麻烦，改为在spring配置中添加统一处理乱码配置
    @RequestMapping("/j2")
    @ResponseBody//加了这个注解，返回的数据就不会走视图解析器了，会直接返回一个字符窜
    public String json2() throws JsonProcessingException {
        //jackson，ObjectMapper
        ObjectMapper mapper = new ObjectMapper();
        //创建一个对象
        User user = new User("秦疆1号",3,"男");
        String s = mapper.writeValueAsString(user);
        return s;
    }

    @RequestMapping("/j3")
    @ResponseBody//加了这个注解，返回的数据就不会走视图解析器了，会直接返回一个字符窜
    public String json3() throws JsonProcessingException {
        //创建一个对象列表
        List<User> list = new ArrayList<User>();
        User user1 = new User("秦疆1号", 3, "男");
        User user2 = new User("秦疆2号", 3, "男");
        User user3 = new User("秦疆3号", 3, "男");
        User user4 = new User("秦疆4号", 3, "男");
        list.add(user1);
        list.add(user2);
        list.add(user3);
        list.add(user4);

        String str = JsonUtils.getJson(list);
        return str;
    }

    @RequestMapping("/j4")
    @ResponseBody//加了这个注解，返回的数据就不会走视图解析器了，会直接返回一个字符窜
    public String json4() throws JsonProcessingException {
        //ObjectMapper，时间解析后的默认格式为:Timestamp，时间戳
        //创建时间一个对象，java.util.Date
        Date date = new Date();
        //将我们的对象解析成为json格式
        String str = JsonUtils.getJson(date);

        return str;
    }

    @RequestMapping("/j5")
    @ResponseBody//加了这个注解，返回的数据就不会走视图解析器了，会直接返回一个字符窜
    public String json5() throws JsonProcessingException {
        List<User> list = new ArrayList<User>();
        User user1 = new User("秦疆1号", 3, "男");
        User user2 = new User("秦疆2号", 3, "男");
        User user3 = new User("秦疆3号", 3, "男");
        User user4 = new User("秦疆4号", 3, "男");
        list.add(user1);
        list.add(user2);
        list.add(user3);
        list.add(user4);

        String str = JSON.toJSONString(list);
        return str;
    }

    @RequestMapping("/j6")
    public String json6() throws JsonProcessingException{
        List<User> list = new ArrayList<User>();
        User user1 = new User("秦疆1号", 3, "男");
        User user2 = new User("秦疆2号", 3, "男");
        User user3 = new User("秦疆3号", 3, "男");
        User user4 = new User("秦疆4号", 3, "男");
        list.add(user1);
        list.add(user2);
        list.add(user3);
        list.add(user4);

        System.out.println("*******Java对象 转 JSON字符串*******");
        String str1 = JSON.toJSONString(list);
        System.out.println("JSON.toJSONString(list)==>"+str1);
        String str2 = JSON.toJSONString(user1);
        System.out.println("JSON.toJSONString(user1)==>"+str2);

        System.out.println("\n****** JSON字符串 转 Java对象*******");
        User jp_user1=JSON.parseObject(str2,User.class);
        System.out.println("JSON.parseObject(str2,User.class)==>"+jp_user1);

        System.out.println("\n****** Java对象 转 JSON对象 ******");
        JSONObject jsonObject1 = (JSONObject) JSON.toJSON(user2);
        System.out.println("(JSONObject) JSON.toJSON(user2)==>"+jsonObject1.getString("name"));

        System.out.println("\n****** JSON对象 转 Java对象 ******");
        User to_java_user = JSON.toJavaObject(jsonObject1, User.class);
        System.out.println("JSON.toJavaObject(jsonObject1, User.class)==>"+to_java_user);

        return "test";
    }

}
